/**
 * 	 Copyright (c) 2008, 2009 Lukas Zaruba
 * 
 *   This file is part of UniAnalyzer (Universal Analyzer).
 *
 *   UniAnalyzer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UniAnalyzer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with UniAnalyzer. If not, see <http://www.gnu.org/licenses/>.
 */


package net.zarubsys.unianalyzer.dataminers.lpt.gui.parameterObject;

import java.util.List;

import net.zarubsys.unianalyzer.dataminers.lpt.pin.CustomizedPin;
import net.zarubsys.unianalyzer.dataminers.lpt.wizard.utils.LPTConsts;

/**
 * MeasuringSettingsFactory
 *
 * @author  &lt;A HREF=&quot;mailto:lukas.zaruba@gmail.com&quot;&gt;Lukas Zaruba&lt;/A&gt
 * @version $Revision$ $Date$
 */
public class MeasuringSettingsFactory {
	private static final boolean STST_STOP_BY_TIME = true;
	private static final boolean STST_RISING_EDGE = true;
	private static final boolean STST_USE_START_TRIGGER = false;
	private static final int STST_SAMPLE_COUNT = 1000;
	private static final int STST_MEASURE_TIME_UNITS_INDEX = 2;
	private static final int STST_MEASURE_TIME = 10;
	private static final MeasuringSettingsFactory INSTANCE = new MeasuringSettingsFactory();
	private LPTMeasuringSettings ms;
	
	private static final int PORT_OFFSET = 0;
	private static final int PORT_ADDRESS_INDEX = 0;
	private static final int PORT_FREQ_UNITS_INDEX = 0;
	private static final int PORT_FREQ = 10;
	
	private static final int STST_MAX_WAIT = 100;
	private static final int STST_MAX_WAIT_UNITS_INDEX = 1;
	
	private MeasuringSettingsFactory() {
		generateDefault();
	}
	
	public static MeasuringSettingsFactory getInstance() {
		return INSTANCE;
	}
	
	public LPTMeasuringSettings getDefaultSettings() {
		try {
			return (LPTMeasuringSettings) ms.clone();
		} catch (CloneNotSupportedException e) {
			throw new RuntimeException("Error creating Default mesuring settings.", e); //$NON-NLS-1$
		}
	}
	
	private void generateDefault() {
		ms = new LPTMeasuringSettings();
		PreviewData previewData = PreviewData.getPreviewData(PORT_OFFSET);
		ms.setCustomizedPins(PreviewData.createCustomizedPins(previewData));
		setupPortSettings(ms.getPortSettings());
		setupStartStopData(ms.getStartStopData());
	}
	
	private void setupStartStopData(StartStopData ssd) {
		ssd.setMaxWaitingInterval(STST_MAX_WAIT);
		ssd.setMaxWaitingIntervalUnits(LPTConsts.getTimeUnitsValues()[STST_MAX_WAIT_UNITS_INDEX]);
		ssd.setMeasureTime(STST_MEASURE_TIME);
		ssd.setMeasureTimeUnits(LPTConsts.getTimeUnitsValues()[STST_MEASURE_TIME_UNITS_INDEX]);
		ssd.setSampleCount(STST_SAMPLE_COUNT);
		ssd.setUseStartTrigger(STST_USE_START_TRIGGER);
		ssd.setRisingEdge(STST_RISING_EDGE);
		ssd.setStopByTime(STST_STOP_BY_TIME);
		List<CustomizedPin> pins = ms.getCustomizedPins();
		for (CustomizedPin pin : pins) {
			if (!pin.isReserved() && pin.isChecked()) {
				ssd.setStartTriggerPin(pin);
				break;
			}
		}
	}

	private void setupPortSettings(PortSettings ps) {
		ps.setPortNumber(LPTConsts.getAddressValues()[PORT_ADDRESS_INDEX]);
		ps.setPortOffset(PORT_OFFSET);
		ps.setUnitsComboValue(LPTConsts.getFrequencyUnitsValues()[PORT_FREQ_UNITS_INDEX]);
		ps.setUnitsComboValueDescription(LPTConsts.getFrequencyUnitsLabels()[PORT_FREQ_UNITS_INDEX]);
		ps.setFrequency(PORT_FREQ);
	}
	
}


/*
 * $Log$
 */